<?php
if(!defined('INCLUDE')) exit();

// Require needed files
require_once CORE_DIRECTORY.OS_PATH_SEPARATOR.'baseThumbnail.php';
require_once CORE_DIRECTORY.OS_PATH_SEPARATOR.'file.php';

$pluginList = array();
// Function which load needed file to allow plugin instantiation
// Populate the $pluginList array
function loadPlugin_thumbnail($e)
{
	global $pluginList;
	if(is_dir($e) && file_exists($e.OS_PATH_SEPARATOR.'index.php'))
	{
		debug($e.OS_PATH_SEPARATOR.'index.php', 'File "%v" loaded', JGLIB_DEBUG_INFO);
		include_once $e.OS_PATH_SEPARATOR.'index.php';
		$pluginList[] = 'thumbnailPlugins_'.$pluginName;
	}
}

// Return the pluginList sorted by priority (see BaseThumbnail::getPriority())
function sortPlugin_thumbnail($e1, $e2)
{
	$v1 = $v2 = 0;
	eval(debug('$v1 = '.$e1.'::getPriority();', 'eval code', JGLIB_DEBUG_EVAL));
	eval(debug('$v2 = '.$e2.'::getPriority();', 'eval code', JGLIB_DEBUG_EVAL));
	if($v1 < $v2)
	{
		return 1;
	}
	return -1;
}

// Scan plugin directory and load them
$scan = new Scandirr(THUMBNAIL_PLUGINS_DIRECTORY, 'loadPlugin_thumbnail', 1);

if(empty($pluginList))
{
	throw new Exception('No thumbnailPlugins found');
}

// Do the sorting see ahead
usort($pluginList, 'sortPlugin_thumbnail');

// For each file which are handled (from upload plugin)
if(isForeachable($files)) foreach($files as $file)
{
	foreach($pluginList as $plugin)
	{
		$p = new $plugin($file);
		// Is this plugin can handled this file ?
		// If so do the processing and BREAK! So only the first plugin is executed
		if(
			$p->getExtensionHandle() === true
			|| in_array($file->getExtension(), $p->getExtensionHandle()) 
			|| in_array($file->getMimeType(),  $p->getMimeHandle())
		)
		{
			// Processing
			debug($p->getThumbnail(),'Plugin "'.$p->getPluginName().'" launched for file "'.$file.'", Result: <a href="%v">here</a>', JGLIB_DEBUG_PER2);
			break;
		}
	}
}
